package
{
	import alternativa.gui.enum.Align;
	import alternativa.gui.layout.LayoutManager;
	import alternativa.gui.mouse.CursorManager;
	import alternativa.gui.mouse.MouseManager;
	import alternativa.gui.theme.defaulttheme.controls.buttons.Button;
	import alternativa.gui.theme.defaulttheme.controls.text.TextArea;
	import alternativa.gui.theme.defaulttheme.init.DefaultTheme;
	import alternativa.gui.theme.defaulttheme.primitives.base.Hint;
	import alternativa.gui.theme.defaulttheme.skin.Cursors;
	import alternativa.init.GUI;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	
	[SWF(backgroundColor="#999999", frameRate="24", width="300", height="500")]
	public class UIInterface extends Sprite
	{
		private var objectContainer:Sprite;
		private var hintContainer:Sprite;
		
		public function UIInterface()
		{
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
		}
		
		private function onAdded(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			init();
		}
		
		internal function init():void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.stageFocusRect = false;
			stage.quality = StageQuality.HIGH;
			
			this.mouseEnabled = false;
			this.tabEnabled = false;
			
			// Container with objects
			objectContainer = new Sprite();
			objectContainer.mouseEnabled = false;
			objectContainer.tabEnabled = false;
			addChild(objectContainer);
			
			// Hint container
			hintContainer = new Sprite();
			hintContainer.mouseEnabled = false;
			hintContainer.tabEnabled = false;
			addChild(hintContainer);
			
			// AlternativaGUIDefaultTheme initialization
			DefaultTheme.init();
			
			// AlternativaGUI initialization
			GUI.init(stage);
			GUI.logoAlign = Align.BOTTOM_LEFT;
			GUI.logoHorizontalMargin = 19;
			GUI.logoVerticalMargin = 10;
			
			// LayoutManager initialization
			LayoutManager.init(stage, [objectContainer, hintContainer]);
			
			// Add hint class to the MouseManager
			MouseManager.setHintImaging(hintContainer, new Hint());
			
			// CursorManager initialization
			CursorManager.init(Cursors.createCursors());
			
			createUIs();
			
			initEvents();
		}
		
		private var textArea:TextArea = null;
		private function createUIs():void
		{
			var button:Button = new Button();
			button.label = "我是按钮";
			objectContainer.addChild(button);
			button.width = 120;
			button.height = 30;
			button.addEventListener(MouseEvent.CLICK,onClick);
			
			textArea = new TextArea();
			textArea.width = 300;
			textArea.height = 400;
			textArea.y = 150;
			objectContainer.addChild(textArea);
		}
		
		private function initEvents():void
		{
			if(ExternalInterface.available)
			{
				ExternalInterface.addCallback("onExternalCall",onExternalCall);
			}
		}
		
		private function onExternalCall(...rest):void
		{
			for(var i:int=0;i<rest.length;++i)
			{
				textArea.text+=rest[i]+"\n";
			}
			
		}
		
		private function onClick(evt:MouseEvent):void
		{
			if(ExternalInterface.available)
			{
				ExternalInterface.call("axShockwaveFlash1_FlashCall",1,2,3);
			}			
		}
		
	}
}